package com.leyou.timer.job;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.leyou.common.aliyunoss.AliyunUtils;
import com.leyou.common.constants.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Set;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Component
public class CleanImageJob {
    @Autowired
    private RedisTemplate redisTemplate;

    public void cleanImage(){
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //获取redis集合中的图片信息
        Set<String> members = redisTemplate.opsForSet().members(RedisConstants.IMAGE_NAMES);
        if (members.size() != 0) {
            for (String imgName : members) {
                //   遍历 图片  判断 redis 该图片有没有存在
                boolean existsKey = redisTemplate.hasKey(RedisConstants.IMAGE_NAME+imgName);
                if (existsKey == false) {
                    //   set集合中没找到  redis 对应得图片信息  那么通知 oss删除图片即可
                    AliyunUtils.deleteFile(imgName);
                    System.out.println("-----阿里云删除垃圾图片一次----");
                    //  set    集合  垃圾图片 删除
                    redisTemplate.opsForSet().remove(RedisConstants.IMAGE_NAMES,imgName);
                }
            }
        }
    }
}
